package com.jiayang.hhotel.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.CleanListAdapter;
import com.jiayang.hhotel.adapter.RoundsListAdapter;
import com.jiayang.hhotel.bean.CleanModel;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.RoundsModel;
import com.jiayang.hhotel.presenter.RoundsHisPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/18 0018.
 * 查房/清洁历史页面
 */

public class RoundsHisActivity extends XActivity<RoundsHisPresenter> {

    private View layout;
    private ImageView icon;
    private TextView tv;

    public static final String HISTORY_FLAG_CLEAN_OR_ROUNDS="HISTORY_FLAG_CLEAN_OR_ROUNDS";
    public static final int INTENT_FLAG_CLEAN = 10000000;//标记从清洁页面跳转至此页
    public static final int INTENT_FLAG_ROUNDS = 10000001;//标记从查房页面跳转到此页

    private SwipeRefreshLayout refreshLayout;
    private SwipeMenuRecyclerView recyclerView;//列表控件

    private RoundsListAdapter adapter;
    private CleanListAdapter cleanListAdapter;

    private int page = 1;

    private int flag;//判断来自哪个页面的历史

    @Override
    public void initData(Bundle savedInstanceState) {
        flag = getIntent().getIntExtra(HISTORY_FLAG_CLEAN_OR_ROUNDS,INTENT_FLAG_CLEAN);
        if(flag==INTENT_FLAG_ROUNDS){
            initTitle(this,true,getString(R.string.rounds),R.color.orange_color,null,null);
        }else{
            initTitle(this,true,getString(R.string.clean),R.color.orange_color,null,null);
        }
        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        refreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        recyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        isLoadMore = true;
        isFresh(true);
        async();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //todo 下拉刷新
                if(!isLoadMore){
                    page = 1;
                    async();
                }else{
                    isFresh(false);
                }
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
                    if (!isLoadMore) {
                        isLoadMore = true;
                        page++;
                        async();
                    }
                }
            }
        });
    }

    private void async(){
        if(flag==INTENT_FLAG_ROUNDS){
            getP().asyncRoundsData(Constant.LAUGUAGE_FLAG_CHINESE,1,page);
        }else{
            getP().asyncCleanData(Constant.LAUGUAGE_FLAG_CHINESE,1,page);
        }
    }

    public void isFresh(final boolean is){
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(is);
            }
        });
    }

    private boolean isLoadMore;//判断是否正在加载。用于防止加载更多时重复加载
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        isFresh(false);
        isLoadMore = false;
        if(page == 1){//只有第一次加载不出数据时才会显示占位图
            refreshLayout.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(R.mipmap.ic_error);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            refreshLayout.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            isFresh(true);
                            async();
                        }
                    });
                    break;
                default://其他错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.net_error));
                    icon.setOnClickListener(null);
                    break;
            }
        }else{
            ToastUtil.Toast(StringUtil.errMsg(state),this);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_rounds;
    }

    @Override
    public RoundsHisPresenter newP() {
        return new RoundsHisPresenter();
    }

    public void setRoundsAdapter(List<RoundsModel.PCheckRoomListDtosBean> resList){
        isFresh(false);
        if (adapter == null) {
            adapter = new RoundsListAdapter(this, resList, false, page, null);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    public void setCleanAdapter(List<CleanModel.PCleanRoomListDtosBean> resList){
        isFresh(false);
        if (cleanListAdapter == null) {
            cleanListAdapter = new CleanListAdapter(this, resList, false,page, null);
            recyclerView.setAdapter(cleanListAdapter);
        } else {
            cleanListAdapter.notifyDataSetChanged();
        }
    }
}
